package cn.edu.ujn.finaltest.service;


import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.finaltest.dao.BaseDict;
import cn.edu.ujn.finaltest.dao.BaseDictMapper;
import cn.edu.ujn.finaltest.dao.Customer;
import cn.edu.ujn.finaltest.dao.CustomerMapper;
import cn.edu.ujn.finaltest.util.Page;
@Service("baseDictService")
@Transactional
public class BaseDictServiceImpl implements IBaseDictService {

	@Autowired
	private BaseDictMapper baseDictDao;
	@Autowired
	private CustomerMapper customerDao;
	
	/**
     * 
     * @param dictId 待删除的数据字典的ID
     * @return 删除成功返回1 删除失败返回0
     */
	@Transactional
	@Override
	public int deleteByPrimaryKey(String dictId) {
		// TODO Auto-generated method stub
		int deleteSuccess = 0;
		
		// 第一步：更新customer表中dictId对应的部分(cust_source、cust_industry或cust_level)为null
		Customer customer = new Customer();
		// 删除的dictId是客户行业(cust_industry)
		customer.setCust_industry(dictId);	
		List<Customer> customerUpdateIndustryList = customerDao.selectCustomerListNoMapping(customer);
		// 删除的dictId是客户信息来源(cust_source)\
		customer.setCust_industry(null);
		customer.setCust_source(dictId);
		List<Customer> customerUpdateSourceList = customerDao.selectCustomerListNoMapping(customer);
		// 删除的dictId是客户等级(cust_level)
		customer.setCust_source(null);
		customer.setCust_level(dictId);
		List<Customer> customerUpdateLevelList = customerDao.selectCustomerListNoMapping(customer);
		if (customerUpdateIndustryList.size() != 0) {
			// 更新客户行业
			System.out.println(customerUpdateIndustryList);
			for (Customer custTemp : customerUpdateIndustryList) {
				custTemp.setCust_industry(null);
				customerDao.updateByPrimaryKey(custTemp);
			}
		} else if (customerUpdateSourceList.size() != 0) {
			// 更新客户信息来源
			System.out.println(customerUpdateIndustryList);
			for (Customer custTemp : customerUpdateIndustryList) {
				custTemp.setCust_source(null);
				customerDao.updateByPrimaryKey(custTemp);
			}
		} else if (customerUpdateLevelList.size() != 0) {
			// 更新客户等级
			System.out.println(customerUpdateIndustryList);
			for (Customer custTemp : customerUpdateIndustryList) {
				custTemp.setCust_level(null);
				customerDao.updateByPrimaryKey(custTemp);
			}
		} else {
			// 不是客户表中使用的数据字典，不需要更新
			System.out.println("无需更新customer表");
		}
		// 第二步：删除数据字典表中对应dictId的条目
		deleteSuccess = baseDictDao.deleteByPrimaryKey(dictId);
		
		return deleteSuccess;
	}

	/**
	 * 数据字典中存在null字段，不使用insert进行插入
	 */
	@Override
	public int insert(BaseDict row) {
		// TODO Auto-generated method stub
		return this.baseDictDao.insert(row);
	}

	@Override
	public int insertSelective(BaseDict row) {
		// TODO Auto-generated method stub
		// 传递dict_type_code、dict_type_name、dict_item_name\个属性即可实现插入
		List<BaseDict> selectAll = this.baseDictDao.selectAll();
		int size = selectAll.size();
		int dict_id = 1;
		int dict_sort;
		
		// 根据dict_id升序排序
		Collections.sort(selectAll);
		for (BaseDict bd : selectAll) {
			// 找到还没有被数据库使用的dict_id
			int currentId = Integer.parseInt(bd.getDict_id());
			if (currentId != dict_id) {
				// 如果循环中某一个被使用的id(升序排序)，和当前累加的id不等，说明该累加id还没有被数据库使用，找到了可以使用的id
				break;
			} else {
				// id已被数据库使用，
				dict_id++;
			}
		}
		List<BaseDict> selectBaseDictByTypeCode = this.baseDictDao.selectBaseDictByTypeCode(row.getDict_type_code());
		dict_sort = selectBaseDictByTypeCode.size() + 1;
		
		row.setDict_id(dict_id + "");
		row.setDict_sort(dict_sort);
		
		return this.baseDictDao.insertSelective(row);
	}

	@Override
	public BaseDict selectByPrimaryKey(String dictId) {
		// TODO Auto-generated method stub
		return this.baseDictDao.selectByPrimaryKey(dictId);
	}
	

	
	/**
	 * selective表示如果某一属性为null，则忽略更新(动态SQL)，controller调用更新用这个方法
	 * dict_item_name和dict_enable(某一type下的的具体item，如客户行业下的教育培训)是两个需要修改的属性
	 * 说明：为什么其他属性(type_code...)可以不变？
	 * 因为客户行业只能对应一个具体职业，而不需要把某个具体职业修改到年营业额、公司性质等type中(没有意义)
	 */
	@Override
	public int updateByPrimaryKeySelective(BaseDict row) {
		// TODO Auto-generated method stub
		return this.baseDictDao.updateByPrimaryKeySelective(row);
	}
	
	/**
	 * 根据主键id进行更新
	 */
	@Override
	public int updateByPrimaryKey(BaseDict row) {
		// TODO Auto-generated method stub
		return this.baseDictDao.updateByPrimaryKey(row);
	}

	/**
	 * 查询所有
	 */
	@Override
	public List<BaseDict> selectAll() {
		// TODO Auto-generated method stub
		BaseDict baseDict = new BaseDict();
		List<BaseDict> selectAll = this.baseDictDao.selectBaseDictList(baseDict);
		return selectAll;
	}

	
	@Override
	public List<BaseDict> selectBaseDictList(BaseDict baseDict) {
		// TODO Auto-generated method stub
		List<BaseDict> selectBaseDictList = this.baseDictDao.selectBaseDictList(baseDict);
		return selectBaseDictList;
	}

	@Override
	public List<BaseDict> selectBaseDictByTypeCode(String typecode) {
		// TODO Auto-generated method stub
		List<BaseDict> selectBaseDictList = this.baseDictDao.selectBaseDictByTypeCode(typecode);
		return selectBaseDictList;
	}

	@Override
	public Page<BaseDict> findBaseDictList(Integer page, Integer rows, String baseDictName, String baseDictTypeName) {
		// TODO Auto-generated method stub
		BaseDict baseDict = new BaseDict();
		// 设置查询条件
        if(!StringUtils.isNullOrEmpty(baseDictName)) {
        	baseDict.setDict_item_name(baseDictName);
        }
		if(!StringUtils.isNullOrEmpty(baseDictTypeName)){
			baseDict.setDict_type_name(baseDictTypeName);
		}
		// 设置查询结果开始行数
		baseDict.setStart((page-1) * rows) ;
		// 设置查询结果返回条数
		baseDict.setRows(rows);
		// 执行分页查询
		List<BaseDict> baseDicts = this.baseDictDao.selectBaseDictList(baseDict);
		// 查询数据字典列表总记录数
		Integer count = this.baseDictDao.selectBaseDictListCount(baseDict);
		// 创建Page返回对象
		Page<BaseDict> result = new Page<BaseDict>();
		// 结果为page页、rows行，数据集为baseDicts，总共count条数据
		result.setPage(page);
		result.setRows(baseDicts);
		result.setSize(rows);
		result.setTotal(count);
		return result;
	}
	
	
}
